var beiMiCommon = require("BeiMiCommon");
cc.Class({
  extends: beiMiCommon,

  properties: {
    username: {
      default: null,
      type: cc.Label
    },
    goldcoins: {
      default: null,
      type: cc.Label
    },
    cards: {
      default: null,
      type: cc.Label
    },
    girl: {
      default: null,
      type: cc.Node
    },
    useravatar: {
      default: null,
      type: cc.Sprite
    },
    lblNotice: {
      default: null,
      type: cc.Label
    },
    lblNoticeLayout: {
      default: null,
      type: cc.Node
    },

    black:{
      default: null,
      type: cc.Node
    },

    shareDaikaiRoom:{
      default: null,
      type: cc.Prefab
    },

    daikaiRoomList:{
      default: null,
      type: cc.Prefab
    },

    versionLabel:{
      default: null,
      type: cc.Label
    }

  },

  onLoad: function() {
    let self = this;
    cc.beimi.extparams=null;
    cc.beimi.isHasEnterRoom=1;
    cc.beimi.roomid=null;
    this.versionLabel.string = "v"+cc.beimi.versionString;
    console.error("大厅--onLoad---------");
    this.node.on('mousedown', function(event) {
      console.log("场景中的鼠标点击事件--mousedown---------");
    }, self);

    // self.girl.active = false;
    if (this.ready()) {
      if (cc.beimi.user.nickname == null) {
        this.username.string = cc.beimi.user.username;
        cc.loader.load("http://wx.qlogo.cn/mmhead/Q3auHgzwzM7Wz9H4T725hoCkR7dvYQibsgBSHNFYwtPjcfIiaPox3Deg/132?aa=aa.jpg", function(error, res) {
          this.useravatar.spriteFrame = new cc.SpriteFrame(res);
        }.bind(this));
      } else {
        this.username.string = cc.beimi.user.nickname + "  ID:" + cc.beimi.user.username;
        if (cc.beimi.user.avatar) {
          cc.loader.load(cc.beimi.user.avatar, function(error, res) {
            this.useravatar.spriteFrame = new cc.SpriteFrame(res);
          }.bind(this));
        }
      }

      if (cc.beimi.user.userCategory==3||cc.beimi.user.userCategory=="3") {
        this.black.active = true;
      }else {
        this.black.active = false;
      }
      this.pva_format(cc.beimi.user.playerlevel, cc.beimi.user.goldcoins, cc.beimi.user.cards, cc.beimi.user.diamonds, self);
      this.pvalistener(self, function(context) {
        console.error("更新pva");
        context.pva_format(cc.beimi.user.playerlevel, cc.beimi.user.goldcoins, cc.beimi.user.cards, cc.beimi.user.diamonds, context);
      });

      this.refreshNotice();

      //刷新房卡
      var param = {
        token: cc.beimi.authorization,
        versionNumber:cc.beimi.versionNumber
      };

      let socket = this.socket();
      socket.emit("getUserInfo", JSON.stringify(param));
      this.map("getUserInfo", this.getUserInfo_event); //接收了聊天信息
      socket.on("getUserInfo", function(result) {
          console.error("getUserInfo");
        var data = self.parse(result);
        self.route("getUserInfo")(data, self);
      });


      // this.map("proxyCreateRoom", this.proxyCreateRoom_event);
      // socket.on("proxyCreateRoom", function(result) {
      //   var data = self.parse(result);
      //   self.route("proxyCreateRoom")(data, self);
      // });

      //
      // this.map("getProxyHomeInfo", this.getProxyHomeInfo_event);
      // socket.on("getProxyHomeInfo", function(result) {
      //   console.log("getProxyHomeInfo_event===>",result);
      //   var data = self.parse(result);
      //   self.route("getProxyHomeInfo")(data, self);
      // });

     //  socket.on("getProxyHomeInfo", function(result) {
     //    var data = self.parse(result);
     //    console.error("getProxyHomeInfo====>",data);
     //    self.getProxyHomeInfo_event(data,self);
     // });

        console.error("发送searchHaveNotFinishGame====>");
      socket.emit("searchHaveNotFinishGame", cc.beimi.authorization);
      socket.on("searchHaveNotFinishGame", function(result) {
        /**
          * 获取是否有正在游戏的状态
          */
            console.error("searchHaveNotFinishGame====>",result);
      var data = self.parse(result);
      if (data.type==1||data.type=="1") {
          cc.beimi.isHasEnterRoom=1 //1没有正在游戏的房间
      }else if (data.type==2||data.type=="2") {
          cc.beimi.isHasEnterRoom=2; //2有正在游戏的房间
          cc.beimi.extparams = data;
       }
     });

    }

    //cc.beimi.audio.playBGM("bgFight.mp3");
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
  },

  getUserInfo_event: function(data, context) {
    cc.beimi.user.playerlevel = data.playerlevel;
    cc.beimi.user.goldcoins = data.goldcoins;
    cc.beimi.user.cards = data.cards;
    context.updatepva();
  },

  proxyCreateRoom_event: function(data, context) {
    context.closeOpenWin();
    let dialog =  cc.instantiate(context.shareDaikaiRoom);
    let script =  dialog.getComponent("DaikaiShare");
    script.init(data.roomId);
    cc.beimi.openwin = dialog;
    cc.beimi.openwin.parent = context.root();
  },

  getProxyHomeInfo_event: function(data, context) {
    context.closeOpenWin();
    console.log("getProxyHomeInfo_event===>",data);
    let dialog =  cc.instantiate(context.daikaiRoomList);
    let script =  dialog.getComponent("DaikaiList");
    script.init(data);
    cc.beimi.openwin = dialog;
    cc.beimi.openwin.parent = context.root();
  },

  onKeyDown: function(event) {
    switch (event.keyCode) {
      case cc.KEY.back:
        this.showQuitApp();
        break;
    }
  },

  update: function(dt) {
    var x = this.lblNotice.node.x;
    x -= dt * 100;
    if (x + this.lblNotice.node.width < -600) {
      x = 700;
    }
    this.lblNotice.node.x = x;
  },

  pva_format: function(playerlevel, coins, cards, diamonds, object) {
    if (coins > 9999) {
      var num = coins / 10000;
      object.goldcoins.string = num.toFixed(2) + "万 " + playerlevel;
    } else {
      object.goldcoins.string = coins + " " + playerlevel;
    }
    object.cards.string = cards + "张";
  },

  playToLeft: function() {
    this._girlAnimCtrl = this.girl.getComponent(cc.Animation);
    this._girlAnimCtrl.play("girl_to_left");
  },
  playToRight: function() {
    this._girlAnimCtrl = this.girl.getComponent(cc.Animation);
    this._girlAnimCtrl.play("girl_to_right");
  },

  onDestroy: function() {
    this.closeloadding();
    this.closealert();
    this.cleanmap();
    cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    this.cleanpvalistener();
  },

  refreshNotice: function() {
    if (cc.beimi.announcement != null && cc.beimi.announcement != '') {
      this.lblNotice.string = cc.beimi.announcement;
    } else {
      this.lblNoticeLayout.active = false;
    }
  },

});
